﻿using FRMS.Helper;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.DAL
{
    public class HomeDAL
    {
        public List<MenuInfo> QueryMenuInfo()
        {
            string strSql = "SELECT * FROM menu_info";

            return SqlHelper<MenuInfo>.SqlList(strSql);
        }

        public User GetUser(string LoginName, string LoginPwd)
        {
            string strSql = "SELECT a.*,b.apply_type FROM user_info a LEFT JOIN project_info b ON a.projectID=b.ID WHERE BINARY a.LoginName='" + LoginName + "' AND BINARY a.LoginPwd='" + LoginPwd + "' AND a.IsDelete=0 ";

            return SqlHelper<User>.SqlSingleData(strSql);
        }
        public User CheckName(string LoginName)
        {
            string strSql = "SELECT * FROM user_info WHERE BINARY LoginName='" + LoginName + "' AND IsDelete=0";

            return SqlHelper<User>.SqlSingleData(strSql);
        }
        public User GetUser(int id)
        {
            if (id == -1)
            {
                User user = new User();
                user.LoginName = "fstar";
                user.LoginPwd = "fstar";
                user.name = "超级管理员";
                return user;
            }
            else
            {
                string strSql = "SELECT * FROM user_info WHERE ID=" + id;
                return SqlHelper<User>.SqlSingleData(strSql);
            }
        }
        public List<Menu> GetMenuByPid(int projectid, string name)
        {
            string strsql;
            if (name == "fstar")
            {
                strsql = "SELECT * FROM menu_info";
            }
            else
            {
                strsql = "SELECT * FROM menu_info WHERE FIND_IN_SET(ID ,(SELECT MenuRole FROM project_info WHERE ID=" + projectid + ")) AND IsDelete=0";
            }
            return SqlHelper<Menu>.SqlList(strsql);
        }

        public List<Menu> GetMenuByPid(int projectid)
        {

            string strsql = "SELECT * FROM menu_info WHERE FIND_IN_SET(ID ,(SELECT MenuRole FROM project_info WHERE ID=" + projectid + ")) AND IsDelete=0";
            return SqlHelper<Menu>.SqlList(strsql);
        }

        public List<FOption> GetFoptionByUser(int userid, string url, string username)
        {
            string strsql;
            if (username == "fstar")
            {
                strsql = "SELECT * FROM vi_foption WHERE IsDelete=0 AND URL='" + url + "'";
            }
            else
            {
                strsql = "SELECT * FROM vi_foption WHERE FIND_IN_SET(ID ,(SELECT foption  FROM user_info WHERE ID=" + userid + ")) AND IsDelete=0 AND URL='" + url + "'";
            }
            return SqlHelper<FOption>.SqlList(strsql);
        }

        public int GetDefaultProject()
        {
            string strsql = "SELECT ID FROM project_info WHERE Isdefault=1";
            return SqlHelper<FOption>.SqlScalar(strsql);
        }

        public ProjectInfo GetDefaultProjectInfo()
        {
            string strsql = "SELECT * FROM project_info WHERE Isdefault=1";
            return SqlHelper<ProjectInfo>.SqlSingleData(strsql);
        }

        public List<FOption> GetAllFoption(int userid)
        {
            string strsql = string.Empty;
            if (userid != 0)
            {
                strsql = "SELECT * FROM vi_foption WHERE FIND_IN_SET(ID ,(SELECT foption FROM user_info WHERE ID=" + userid + ")) AND IsDelete=0";
            }
            else
            {
                strsql = "SELECT * FROM vi_foption WHERE IsDelete=0";
            }
            return SqlHelper<FOption>.SqlList(strsql);
        }

        public ProjectInfo CheckSession(string session)
        {
            string strSql = "select * FROM project_info WHERE `Session`='" + session + "'";
            return SqlHelper<ProjectInfo>.SqlSingleData(strSql);
        }
    }
}
